/**
 * Course			:	ISYS2119 - BIS Development 2
 * Assignment		:	02
 * Date due			:	01/08/2012
 * Program 			:	Saigon Bike Rental Catalogue System
 * 
 * Programmer		:	Vuu Ngoc My Linh, Tran Thi Van Anh
 * StudentID     	:	s3342015, s3342025
 * Attributions		:	Lecture slides, Blackboard, Tutorials, Websites
 * ====================================================================
 * Description: To validate input		
 */

package rmit.ISYS2119.assign02;

import java.util.Calendar;

public class Validation {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * Check if the BikeId is unique
	 */
	public static boolean notInBikeIdList(int bikeId, int[] bikeIdList) {

		// Do not check if it is the first record
		if (bikeIdList.length == 0)
			return true;

		// Check the bikeId with the array list
		for (int i = 0; i < bikeIdList.length; i++)

			// If duplicate
			if (bikeId == bikeIdList[i])
				return false;

		return true;
	}

	/**
	 * Check if the Model Name is valid
	 */
	public static boolean isNameValid(String name) {

		// To temporarily hold characters to be checked
		char temp;

		boolean isNameValid = true;

		// Name can not be null
		if (name.equals("")) {
			isNameValid = false;
		}

		// Check each character of the string Name
		for (int i = 0; i < name.length(); i++) {
			temp = name.charAt(i);
			int ascii = (int) temp;

			// Name must contain only alphabets or spaces
			if (ascii > 64 & ascii < 91 || ascii > 96 & ascii < 123 || (i > 0)
					&& (ascii == 32)) {
				isNameValid = true;
				break;
			} else {
				isNameValid = false;
			}
		}

		return isNameValid;
	}

	/**
	 * Check if the Year is valid
	 */
	public static boolean isYearValid(int year) {
		boolean isYearValid = false;

		// Get Calendar
		Calendar calendar = Calendar.getInstance();

		// Get current year
		int currentYear = calendar.get(Calendar.YEAR);

		// Validate the year
		if (year < currentYear && year > 1999) {
			isYearValid = true;
		}

		return isYearValid;
	}

	/**
	 * Check if the number of bikes is a positive number
	 */
	public static boolean validateNumberOfBikes(int numberOfBikes) {

		// Can not < 0
		if (numberOfBikes <= 0) {
			return false;
		}
		return true;
	}

	/**
	 * Check if the BikeId is from 0001 to 9999
	 */
	public static boolean validateBikeID(int bikeID) {

		// can not <= 0000 or > 9999
		if (bikeID <= 0000 || bikeID > 9999) {
			return false;
		}
		return true;
	}

	/**
	 * Check if the BikeId is number
	 */
	public static boolean isNumber(String bikeID) {
		try {

			// Try to Convert bikeID to integer
			Integer.parseInt(bikeID);
			return true;

		} catch (Exception e) {

			// If it can not be converted, it is not a number
			return false;
		}

	}

	/**
	 * Check if the Rental cost is in Double type
	 */
	public static boolean isDouble(String rentalCost) {
		try {

			// Try to Convert bikeID to double
			Double.parseDouble(rentalCost);
			return true;
		} catch (Exception e) {

			// If it can not be converted, it is not Double
			return false;
		}

	}

}
